package servlet

import com.google.gson.Gson
import javax.servlet.http.HttpServlet
import javax.servlet.http.HttpServletRequest
import javax.servlet.http.HttpServletResponse

open class MyHttpServlet : HttpServlet() {
    override fun doGet(req: HttpServletRequest, resp: HttpServletResponse) {
        req.characterEncoding = "UTF-8"
        resp.characterEncoding = "UTF-8"
        resp.setHeader("content-type", "text/html;charset=utf-8");
    }

    override fun doPost(req: HttpServletRequest, resp: HttpServletResponse) {
        req.characterEncoding = "UTF-8"
        resp.characterEncoding = "UTF-8"
        resp.setHeader("content-type", "text/html;charset=utf-8");
    }
}

fun HttpServletResponse.returnSuc(info: String = "", content: String = "") {
    val responseInfo = ResponseInfo()
    responseInfo.msg = info
    responseInfo.content = content
    val json: String = Gson().toJson(responseInfo)
    writer.println(json)
    writer.flush()
    writer.close()
}

fun HttpServletResponse.returnErr(errInfo: String) {
    val responseInfo = ResponseInfo()
    responseInfo.rel = 1
    responseInfo.msg = errInfo
    val json: String = Gson().toJson(responseInfo)
    writer.println(json)
    writer.flush()
    writer.close()
}